package cn.qdu.controller;

import cn.qdu.dao.LikeDao;
import cn.qdu.model.Like;
import cn.qdu.model.User;
import cn.qdu.service.LikeService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;


@WebServlet("/likes")
public class LikeServlet extends HttpServlet {
    private LikeService likeService;

    @Override
    public void init() throws ServletException {
        this.likeService = new LikeService(new LikeDao());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String postIdStr = request.getParameter("postId");
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        if (currentUser == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            out.write("{\"error\":\"请先登录\"}");
            return;
        }

        try {
            int postId = Integer.parseInt(postIdStr);
            boolean result = likeService.toggleLike(postId, currentUser.getUserId());
            int likeCount = likeService.getLikeCount(postId);

            // 返回JSON响应
            String jsonResponse = String.format(
                    "{\"success\":true,\"liked\":%b,\"likeCount\":%d}",
                    result, likeCount
            );
            out.write(jsonResponse);

            System.out.println("Received postId: " + postIdStr); // 调试
        } catch (NumberFormatException e) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            out.write("{\"error\":\"参数错误\"}");
        } catch (SQLException e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.write("{\"error\":\"数据库错误\"}");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String postIdStr = request.getParameter("postId");

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        try {
            int postId = Integer.parseInt(postIdStr);
            List<Like> likes = likeService.getLikes(postId);

            // 使用Gson转换为JSON
            Gson gson = new Gson();
            out.write(gson.toJson(likes));
        } catch (NumberFormatException e) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            out.write("{\"error\":\"参数错误\"}");
        } catch (SQLException e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.write("{\"error\":\"数据库错误\"}");
        }
    }
}